<template>
  <div class="params">
    <breadcrumb :first="'商品管理'"></breadcrumb>
    <el-card>
      <el-tag type="warning">
        <i class="el-icon-warning" style="color: orange"></i>注意:
        只允许为第三级分类设置相关参数
      </el-tag>
      <span>选择商品分类</span>
      <!-- 级联选择器 -->
      <el-cascader
        clearable
        v-model="cateValue"
        :options="catelist"
        :props="cascaderProps"
        @change="handleChange"
      ></el-cascader>
      <!-- 动态标签 -->
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button type="primary" :disabled="btnflag" @click="addparams"
            >添加参数</el-button
          >
          <!-- 动态参数表格数据 -->
          <el-table :data="manytableform" border style="width: 100%">
            <el-table-column type="expand"></el-table-column>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column
              prop="attr_name"
              label="参数名称"
            ></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" size="mini">编辑</el-button>
                <el-button type="danger" size="mini">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-button type="primary" :disabled="btnflag" @click="addattr"
            >添加属性</el-button
          >
          <!-- 动态参数表格数据 -->
          <el-table :data="onlytableform" border style="width: 100%">
            <el-table-column type="expand"></el-table-column>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column
              prop="attr_name"
              label="属性名称"
            ></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" size="mini">编辑</el-button>
                <el-button type="danger" size="mini">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 添加参数或属性对话框 -->
    <el-dialog
      :title="paramsOrattr ? '添加参数' : '添加属性'"
      :visible.sync="dialogflag"
      @close="addform = {}"
    >
      <el-form :model="addform" label-width="80px" :rules="rules">
        <el-form-item
          prop="attr_name"
          :label="paramsOrattr ? '参数名称' : '属性名称'"
        >
          <el-input v-model="addform.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogflag = false">取 消</el-button>
        <el-button type="primary" @click="submitparams">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import breadcrumb from "@/components/breadcrumb.vue";
import { categorieslist, getparameterlist, addparamsOrattr } from "@/api/index";
export default {
  data() {
    return {
      catelist: [],
      //级联选择器选择的值
      cateValue: [],
      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        expandTrigger: "hover",
      },
      //激活的页签名称
      activeName: "many",
      //控制添加按钮的禁用状态
      btnflag: true,
      // 级联选择器选中的分类参数id
      cateid: [],
      //获取到的动态参数或静态属性的参数
      manytableform: [],
      onlytableform: [],
      //添加参数或属性对话框
      paramsOrattr: true,
      //对话框开关
      dialogflag: false,
      //规则校验
      rules: {
        attr_name: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
      },
      //添加参数或属性表单
      addform: {},
    };
  },
  components: {
    breadcrumb,
  },
  created() {
    this.getcatelist();
  },
  methods: {
    getcatelist() {
      categorieslist().then((res) => {
        this.catelist = res.data.data;
      });
    },
    //获取动态参数或静态属性
    getparams() {
      getparameterlist(this.cateid, this.activeName).then((res) => {
        if (this.activeName === "many") {
          this.manytableform = res.data.data;
          console.log(this.manytableform);
        } else {
          this.onlytableform = res.data.data;
          console.log(this.onlytableform);
        }
      });
    },
    //级联选择器触发函数
    handleChange() {
      if (this.cateValue.length < 3) {
        this.cateValue = [];
        this.btnflag = true;
        return;
      }
      this.btnflag = false;
      this.cateid = this.cateValue[2];
      this.getparams();
    },
    //触发标签页函数
    handleClick() {
      this.getparams();
    },
    //添加参数或属性
    addparams() {
      this.dialogflag = true;
      this.paramsOrattr = true;
    },
    addattr() {
      this.dialogflag = true;
      this.paramsOrattr = false;
    },
    //提交参数或属性的添加
    submitparams() {
      addparamsOrattr(this.cateid, {
        attr_name: this.addform.attr_name,
        attr_sel: this.activeName,
      }).then((res) => {
        if (res.data.meta.status === 201) {
          this.dialogflag = false;
          this.getparams();
          this.$message({
            message: '添加成功',
            type: 'success'
          })
        } 
      });
    },
  },
};
</script>

<style lang="less" scoped>
.params {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 10px;
  .el-tag {
    width: 100%;
    font-size: 16px;
    margin-bottom: 10px;
  }
}
</style>
